﻿using System;
using System.IO;
using System.Web;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Data.Objects;
using System.Linq;

namespace Exhibition.Common.Extension
{
    public static class JsonHelper
    {

        

        public static string ToJsonItem(this object item)
        {
            return JsonConvert.SerializeObject(item, new IsoDateTimeConverter());
        }

        /// <summary>
        /// 转时间类型
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static DateTime StringToDateTime(this object item)
        {
            if (item == null)
            {
                return DateTime.Now;
            }
            return Convert.ToDateTime(item);
        }
        /// <summary>
        /// 扩展转bool
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool ToBoolean(this object item)
        {
            if (item == null)
            {
                return false;
            }
            return Convert.ToBoolean(item);
        }




        /// <summary>
        /// 扩展转int
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static int ToInt32(this object item  )
        {
            if (item==null)
            {
                return 0;
            }


            return Convert.ToInt32(item);
        }




        public static string ToDateTimeString(this object item, string format)
        {
            if (item == null)
            {
                return "";
            }
            return Convert.ToDateTime(item).ToString(format);
        }

        public static T FromJsonTo<T>(this string jsonString)
        {
            return JsonConvert.DeserializeObject<T>(jsonString, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
        }

        public static string ToTraceString<T>(this IQueryable<T> t)
        {
            ObjectQuery<T> oqt = t as ObjectQuery<T>;
            if (oqt != null)
                return oqt.ToTraceString();
            return "";
        }



    }
}
